using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;

using Ultimate;
using Ultimate.DAL;
using Ultimate.DAL.CollectionClasses;
using Ultimate.DAL.HelperClasses;
using Ultimate.DAL.EntityClasses;
using Ultimate.DAL.FactoryClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace CommunityServerWeb.RegistrationSystem.League.Controls
{
    public partial class Rosters : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(LeagueID != -1)
                BuildRosterReport();

        }

        private int LeagueID
        {
            get
            {
                int id = -1;
                string sid = Request.QueryString["leagueid"];

                if (sid != null && sid.Length > 0)
                {
                    id = int.Parse(sid);
                }

                return id;
            }
        }

        private void BuildRosterReport()
        {
            
            StringBuilder sb = new StringBuilder();
            Ultimate.DAL.TypedListClasses.PlayerTeamLeagueTypedList ptl = new Ultimate.DAL.TypedListClasses.PlayerTeamLeagueTypedList();
            PredicateExpression A = new PredicateExpression();
            A.Add(PredicateFactory.CompareValue(TeamFieldIndex.LeagueID, ComparisonOperator.Equal, LeagueID));

            ISortExpression sorter = new SortExpression(SortClauseFactory.Create(TeamFieldIndex.TeamName, SortOperator.Ascending));

            sorter.Add(SortClauseFactory.Create(TeamPlayerFieldIndex.RoleID, SortOperator.Ascending));
            sorter.Add(SortClauseFactory.Create(PlayerFieldIndex.LastName, SortOperator.Ascending));
            ptl.Fill(0, sorter, false, A);

            string currentTeam = "";
            bool firstRun = true;
            foreach (Ultimate.DAL.TypedListClasses.PlayerTeamLeagueRow row in ptl.Rows)
            {
                if (currentTeam != row.TeamName)
                {
                    currentTeam = row.TeamName;
                    if (!firstRun) sb.Append("</table>");
                    firstRun = false;
                    sb.Append("<h3>" + row.TeamName + "</h3><table>");
                }
                sb.Append("<td>");
                if (row.RoleID == (int)Ultimate.Roles.Captain) sb.Append("C&nbsp;");
                sb.Append(row.PlayerName + "<td>");
                if (ShowPersonalInfo)
                {
                    sb.Append("<td>" + row.Email + "</td>");
                    sb.Append("<td>" + row.PhoneNumber + "</td></tr>");
                }
                else
                {
                    sb.Append("</tr>");
                }
            }
            sb.Append("</table>");

            rosters.Text = sb.ToString();
        }

        private bool _showPersonalInfo = true;

        public bool ShowPersonalInfo
        {
            get { return _showPersonalInfo; }
            set { _showPersonalInfo = value; }
        }
    }
}